package org.suyafei.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.suyafei.manager.ProductOrderManager;
import org.suyafei.mapper.ProductOrderMapper;
import org.suyafei.model.ProductOrderDO;
import org.suyafei.utils.BeanUtil;
import org.suyafei.vo.ProductOrderVO;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 宿某人
 * @Date: 2022/02/05/10:22 下午
 * @Description:
 */
@Component
@Slf4j
public class ProductOrderManagerImpl implements ProductOrderManager {

    @Autowired
    private ProductOrderMapper productOrderMapper;

    @Override
    public int add(ProductOrderDO productOrderDO) {
        int rows = productOrderMapper.insert(productOrderDO);
        return rows;
    }

    @Override
    public ProductOrderDO findByOutTradeNoAndAccountNo(String outTradeNo, Long accountNo) {
        ProductOrderDO productOrderDO = productOrderMapper.selectOne(new QueryWrapper<ProductOrderDO>().eq("out_trade_no", outTradeNo).eq("account_no", accountNo).eq("del", 0));
        return productOrderDO;
    }

    @Override
    public int updateOrderPayState(String outTradeNo, Long accountNo, String newState, String oldState) {
        int rows = productOrderMapper.update(null, new UpdateWrapper<ProductOrderDO>().eq("out_trade_no", outTradeNo).eq("account_no", accountNo).eq("state", oldState).set("state", newState));
        return rows;
    }

    @Override
    public Map<String, Object> page(int page, int size, Long accountNo, String state) {
        Page<ProductOrderDO> pageInfo = new Page<>(page, size);
        IPage<ProductOrderDO> orderDOIPage;
         if (StringUtils.isBlank(state)) {
            orderDOIPage = productOrderMapper.selectPage(pageInfo, new QueryWrapper<ProductOrderDO>().eq("account_no", accountNo).eq("del",0));
        } else {
             orderDOIPage = productOrderMapper.selectPage(pageInfo, new QueryWrapper<ProductOrderDO>().eq("account_no", accountNo).eq("state", state).eq("del", 0));
        }
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("total_record", orderDOIPage.getTotal());
        pageMap.put("total_page", orderDOIPage.getPages());
        pageMap.put("current_data", orderDOIPage.getRecords().stream().map(obj -> BeanUtil.beanProcess(obj, ProductOrderVO.class)).collect(Collectors.toList()));
        return pageMap;
    }

    @Override
    public int del(Long orderId, Long accountNo) {
        int rows = productOrderMapper.update(null, new UpdateWrapper<ProductOrderDO>().eq("id", orderId).eq("account_no", accountNo).set("del", 1));
        return rows;
    }
}
